package han.cirno.corrupt

import android.content.Context
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelStore
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import han.cirno.corrupt.activity.GlobalViewModel
import javax.inject.Inject
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
object GlobalViewModelModule {
    @Provides
    @Singleton
    fun provideGlobalViewModel(
        @ApplicationContext context: Context
    ): GlobalViewModel {
        val factory = ViewModelProvider.AndroidViewModelFactory(context.applicationContext as Application)
        return ViewModelProvider(ViewModelStore(), factory)[GlobalViewModel::class.java]
    }
}

@Module
@InstallIn(SingletonComponent::class)
object AppModule {
    @Provides
    fun provideApplication(application: Application): Application = application
    @Provides
    fun provideApplicationRepository(application: Application): ApplicationRepository =
        ApplicationRepository(application)
}

class ApplicationRepository @Inject constructor(private val application: Application) {
    fun getAppName(): String = application.getString(R.string.app_name)
}